home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15639 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  123 lines

  1. Path: spot.Colorado.EDU!karki
  2. From: karki@spot.Colorado.EDU (KARKI JITENDRA BAHADUR)
  3. Newsgroups: comp.lang.c++
  4. Subject: [Q] Why does this code crash my compiler?
  5. Date: 7 Apr 96 19:30:27 GMT
  6. Organization: University of Colorado at Boulder
  7. Message-ID: <karki.828905427@spot.Colorado.EDU>
  8. NNTP-Posting-Host: spot.colorado.edu
  9. Summary: The code compiles ok piece-by-piece, but crashes as a whole.
  10. Keywords: Watcom template istream conversion
  11. X-Newsreader: NN version 6.5.0 #8 (NOV)
  12.  
  13. The following code compiles and runs ok on Watcom C++ 10.0a if either
  14. of the #defines is commented out, but with both of them in force, it
  15. causes an access violation during compilation.
  16. Is the code really bad enough to crash the compiler, or should I get
  17. a new compiler?  Could anyone try compiling it to see if it's a code
  18. problem or a compiler problem?
  19.  
  20.  
  21. #include <iostream.h>
  22. #define make_xxx
  23. #define inp_xxx
  24.  
  25. template<class stuff>
  26. class XXX
  27. {
  28.    private:
  29.       stuff item;
  30.    public:
  31.       XXX();
  32.       XXX(stuff);
  33.       ~XXX();
  34.       operator int();
  35. #ifdef inp_xxx
  36.       friend istream& operator >>(istream& ins, XXX<stuff>& a);
  37. #endif
  38. };
  39. template<class stuff>
  40. XXX<stuff>::XXX()
  41. {
  42. }
  43. template<class stuff>
  44. XXX<stuff>::XXX(stuff a)
  45. {
  46.    item = a;
  47. }
  48. template<class stuff>
  49. XXX<stuff>::operator int()
  50. {
  51.    return 11;
  52. }
  53. template<class stuff>
  54. XXX<stuff>::~XXX()
  55. {
  56.    cout << "XXX gone" << endl;
  57. }
  58. #ifdef inp_xxx
  59. template<class stuff>
  60. istream& operator >>(istream& ins, XXX<stuff>& a)
  61. {
  62.    cout << "Enter the XXX value:> ";
  63.    stuff item;
  64.    ins >> item;
  65.    a.item = item;
  66.    return ins;
  67. }
  68. #endif
  69.  
  70. template<class stuff>
  71. class YYY
  72. {
  73.    private:
  74.       stuff item;
  75.    public:
  76.       YYY();
  77.       ~YYY();
  78.       operator int();
  79. #ifdef make_xxx
  80.       operator XXX<stuff>();
  81. #endif
  82. };
  83. template<class stuff>
  84. YYY<stuff>::YYY()
  85. {
  86.    item = 0;
  87. }
  88. template<class stuff>
  89. YYY<stuff>::~YYY()
  90. {
  91.    cout << "YYY gone" << endl;
  92. }
  93. template<class stuff>
  94. YYY<stuff>::operator int()
  95. {
  96.    return 7;
  97. }
  98. #ifdef make_xxx
  99. template<class stuff>
  100. YYY<stuff>::operator XXX<stuff>()
  101. {
  102.    XXX<stuff> a(item);
  103.    return a;
  104. }
  105. #endif
  106.  
  107. main()
  108. {
  109.    XXX<int> x;
  110.    YYY<unsigned> y;
  111.    XXX<unsigned> z;
  112. #ifdef inp_xxx
  113.    cin >> x;
  114. #endif
  115.    cout << "Ok so far\n";
  116.    int i;
  117.    i = y;
  118.    cout << i << endl;
  119. #ifdef make_xxx
  120.    z = y;
  121. #endif
  122. }
  123.